package array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class LeetCode4 {
    public static void main(String[] args) {
        LeetCode4 leetCode4 = new LeetCode4();
        double v = leetCode4.findMedianSortedArrays(new int[]{1, 2}, new int[]{3,4});
        System.out.println(v);
    }
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int size = nums1.length + nums2.length;
        int p0 = 0;
        int p1 = 0;
        ArrayList<Integer> re = new ArrayList<>(size);
        while (p0 < nums1.length || p1 < nums2.length){
            if(p0 >= nums1.length){
                re.add(nums2[p1++]);
                continue;
            }else if(p1 >= nums2.length){
                re.add(nums1[p0++]);
                continue;
            }
            if(nums1[p0] < nums2[p1]){
                re.add(nums1[p0++]);
            }else{
                re.add(nums2[p1++]);
            }
        }
        if(size % 2 != 0){
            return re.get(size / 2);
        }else{
            return (re.get(size / 2) + re.get(size / 2 - 1)) / 2.0;
        }
    }
}
